﻿/*
 *   2011.08.22 lcheng 编写
 *   XHeditor upload的cs代码
 * 
 */
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Collections.Generic;

using TRPOP.Config;
using TRPOP.Common;
using TRPOP.CMS.Plugin;
using Newtonsoft.Json;
using TRPOP.CMS.Model;

namespace TRPOP.CMS.Web.UI
{
    public class XHUpload : BaseAdmin
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Charset = "UTF-8";
            string strErr = string.Empty;
            string strMsg = string.Empty;

            if (Session["jsNode"] == null || (Session["jsNode"] is JavaScriptObject) == false)
            {
                strErr = "请通过编辑器上传图片";
            }
            else
            {
                //BaseURL
                string strBaseUrl = "/resource";
                JavaScriptObject jsNode = (JavaScriptObject)Session["jsNode"];
                if (jsNode.ContainsKey("savepath"))
                {
                    strBaseUrl = jsNode["savepath"].ToString();
                    strBaseUrl = strBaseUrl + "/";
                    strBaseUrl = strBaseUrl.Replace("//", "/");
                }
                //upload file......

                // 新的上传图片的方法
                string strFilePath = strBaseUrl.Replace("~/", "/");
                string strUploadFileName = string.Empty;
                string strUploadFileWithPath = string.Empty;
                AttachmentInfo oAttachmentInfo = TRPOP.Common.EditorImgHelper.EditorUploadFile(ref strUploadFileName, ref strUploadFileWithPath, strFilePath, SYSConfig.Attachment_UpImageMaxWidth);
                oAttachmentInfo.FileName = strUploadFileWithPath;

                if (oAttachmentInfo != null && oAttachmentInfo.ErrorDescription.IsEmpty())
                {
                    strMsg = oAttachmentInfo.FileName;
                    strErr = string.Empty;
                }
                else if (oAttachmentInfo != null)
                {
                    strErr = oAttachmentInfo.ErrorDescription;
                }
                else
                {
                    strErr = "处理图片失败";
                }
            }
            Response.Write("{\"err\":\"" + strErr.GetJSONStr() + "\",\"msg\":\"" + strMsg.GetJSONStr() + "\"}");
            Response.End();
        }
    }
}
